package org.example.controller;

import org.apache.commons.lang3.StringUtils;
import org.example.pojo.UmsRuleRelate;
import org.example.service.IUmsRuleRelateService;
import org.example.util.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wy
 * @since 2023-10-05
 */
@RestController
@RequestMapping("/ums-rule-relate")
public class UmsRuleRelateController {
    @Resource
    IUmsRuleRelateService relateService;

    @GetMapping("/result")
    CommonResult getResult(String rule){
        if(rule == null) return CommonResult.failed("条件不能为空");
        Object result = relateService.getResult(getListRules(rule));
        if(result != null){
            return CommonResult.success(result);
        }
        return CommonResult.failed("条件不充足");
    }

    @GetMapping("/recodes")
    CommonResult getRecodes(String rule){
        if(StringUtils.isBlank(rule)) return CommonResult.failed("条件不能为空");
        return CommonResult.success(relateService.getDetails(getListRules(rule), new HashSet<>()));
    }

    @GetMapping("/reverse")
    CommonResult getReverse(String result, String cause){
        if(StringUtils.isBlank(result)) return CommonResult.failed("条件不能为空");
        Object reverseRecodes;
        if(StringUtils.isBlank(cause)){
            reverseRecodes = relateService.getReverseRecodes(result, new HashSet<>());
        }else{
            List<String> listRules = getListRules(cause);
            reverseRecodes = relateService.getReverseRecodes(result, listRules);
        }
        return CommonResult.success(reverseRecodes);
    }

    @GetMapping("/bidirection")
    CommonResult getBidirection(String result, String cause){
        if(StringUtils.isBlank(result) || StringUtils.isBlank(cause)) return CommonResult.failed("条件不能为空");
        Object bidrection = relateService.getBidrection(getListRules(cause), result);
        return CommonResult.success(bidrection);
    }

    @PostMapping("/add")
    CommonResult add(UmsRuleRelate rule){
        rule.setActive(1);
        if(relateService.saveOrUpdate(rule)){
            return CommonResult.success("添加成功");
        }

        return CommonResult.failed("添加失败");
    }
    @PostMapping("/del")
    CommonResult delete(UmsRuleRelate rule){
        rule.setActive(0);
        if(relateService.updateById(rule)){
            return CommonResult.success("删除成功");
        }

        return CommonResult.failed("删除失败");
    }

    @PostMapping("/batchdel")
    CommonResult batchdel(Integer[] ids){
        return CommonResult.success(relateService.batchdel(ids));
    }

    @PostMapping("/update")
    CommonResult update(UmsRuleRelate rule){
        if(relateService.updateById(rule)){
            return CommonResult.success("修改成功");
        }

        return  CommonResult.failed("修改失败");
    }

    @GetMapping("/list")
    CommonResult list(UmsRuleRelate rule){
        return CommonResult.success(relateService.list(rule));
    }

    @PostMapping("/back")
    CommonResult back(UmsRuleRelate rule){
        rule.setActive(1);
        if(relateService.updateByIdTest(rule)){
            return CommonResult.success(true);
        }
        return CommonResult.failed("存在无效规则");
    }

    private List<String> getListRules(String rule){
        if(!rule.contains(",")){
            return Collections.singletonList(rule);
        }

        String[] split = rule.split(",");
        
        return new ArrayList<>(List.of(split));
    }

}
